import employee from '@/api/system/employeeAPI'
import customer from '@/api/crm/customerAPI'
export default {
  data () {
    return {
      employees: [], // 员工信息
      customerChannels: []// 客户渠道
    }
  },
  methods: {
    /**
     *  请求
     * @param opt  add:新增 update:修改 delete:删除
     * @param params
     * @param successBack
     * @param errorBack
     */
    requestOption (opt, params, successBack, errorBack) {
      let requestAPI
      if (opt && opt !== undefined && typeof opt === 'function') {
        requestAPI = opt
      } else {
        requestAPI = this.getRequestType(opt)
        if (!requestAPI) {
          return
        }
      }
      requestAPI(params).then(res => {
        if (successBack && successBack !== null) {
          successBack(res)
        }
      }).catch(error => {
        if (errorBack && errorBack !== null) {
          errorBack(error)
        }
      })
    },
    /**
     *  校验表单
     * @param formName
     * @returns {boolean}
     */
    checkFormData (formName) {
      let f = false
      this.$refs[formName].validate(valid => {
        f = valid
      })
      return f
    },
    // querySearchAsync (queryString, cb) {
    //   let employees = this.employees
    //   let results = queryString ? employees.filter((item) => {
    //     return (item.employeeName.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
    //   }) : employees
    //   cb(results)
    // },
    handleSelect (item) {
      this.formData.chargePerson = item.employeeName
      this.formData.chargePersonCode = item.employeeCode
    },
    handleEmployeeQuery () {
      this.getFilterEmployee(null, (data) => {
        this.employees = data
      })
    },
    /**
     * 查询客户渠道
     */
    getCustomerChannels () {
      customer.getCustomerSourceChannelListAPI().then(res => {
        this.customerChannels = res.data || []
      })
    },
    /**
     * 查询员工
     * @param queryString
     * @param cb
     */
    getFilterEmployee (params, cb) {
      const p = params && params !== undefined ? params : {}
      p.isPage = false
      employee.getEmployeeListAPI(p).then((res) => {
        cb(res.data.list)
      })
    },
    /**
     * 格式化 产品标签 （el-table-column :formatter）
     * @param {*} row
     * @param {*} column
     * @param {*} cellValue
     * @param {*} index
     * @returns
     */
    formatterProductType (row, column, cellValue, index) {
      const productTypes = this.$store.state.common.productTypes
      if (productTypes && productTypes.length > 0) {
        const productTypeInfo = productTypes.find((item) => {
          if (cellValue === item.bizValue) {
            return item
          }
        })
        return productTypeInfo && productTypeInfo !== null ? productTypeInfo.bizName : cellValue
      }
      return cellValue
    },
    /**
     *  格式化员工职位
     * @param {*} cellValue
     * @returns
     */
    formatterPosition (cellValue) {
      if (cellValue) {
        return cellValue.map(v => {
          return v.positionName
        }).join(',')
      }
    },
    /**
     * 合同状态
     */
    getContractStatus () {
      return [ {value: 0, label: '未提交'}, {value: 1, label: '已提交'}, {value: 2, label: '已驳回'}, {value: 3, label: '已通过'}, {value: 4, label: '签署中'}, {value: 5, label: '签署完成'}, {value: 6, label: '签署失败'}, {value: -1, label: '已取消'} ]
    },
    /**
     * 销售订单状态
     */
    getSalesOrderStatus () {
      return [ {value: 0, label: '未提交'}, {value: 1, label: '已提交'}, {value: 2, label: '已驳回'}, {value: 3, label: '已通过'}, {value: 4, label: '已完成'}, {value: -1, label: '已取消'} ]
    },
    /**
     * 销售订单订单类型
     */
    getSalesOrderTypes () {
      return [{value: 0, label: '销售订单'}, {value: 1, label: '预售订单'}]
    },
    getRelationTypes () {
      return [{value: 0, label: '供应商'}, {value: 1, label: '物流公司'}, {value: 2, label: '客户'}, {value: 99, label: '其他相关'}]
    },
    getReceivableRelationTypes () {
      return [{value: 2, label: '客户'}, {value: 0, label: '供应商'}, {value: 99, label: '其他相关'}]
    },
    getReceivableTypes () {
      return [{value: 1, label: '预付金'}, {value: 2, label: '余款'}, {value: 3, label: '尾款'}, {value: 4, label: '一次性全款'}, {value: 5, label: '退款'}]
    },
    getReturnTypes () {
      return [{value: 0, label: '仅退货'}, {value: 1, label: '仅退款'}, {value: 2, label: '退货退款'}]
    },
    getReturnStatus () {
      return [{value: 0, label: '未提交'}, {value: 1, label: '已提交'}, {value: 2, label: '已驳回'}, {value: 3, label: '已通过'}, {value: 4, label: '已完成'}, {value: -1, label: '已取消'}]
    }
  }
}
